package com.dimao.certificate_build_service.Util.Log;


import com.dimao.certificate_build_service.Util.TimeUtil;
import io.vertx.core.Vertx;
import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class TextLogAction implements SaveLogMessageHandler {
    //保存日志文件夹名称
    private static final String pathName = "log";
    //日志文件后缀名
    private static final String address = ".log";

    private Vertx vertx;

    public TextLogAction(Vertx vertx){
        this.vertx = vertx;
    }

    @Override
    public void save(String message) {
        File file = new File(dir() + "/"
                + TimeUtil.getCurrentDate(false) + address);

        try {
            FileWriter fw = new FileWriter(file,true);
            fw.write(message);
            fw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @NotNull
    private String dir(){
        File file = new File(pathName);
        if(!file.exists() || !file.isDirectory()){
            file.mkdir();
        }
        return file.getPath();
    }
}
